#include "options_annealing.h"

/** Nacte nastaveni z daneho XML elementu.
 * \param settings XML element s nastavenim simulovaneho zihani
 */
void AnnealingOptions::Read(ticpp::Element * settings) {
	settings->GetAttributeOrDefault("alpha", &alpha, 0.9);
	settings->GetAttributeOrDefault("all", &all, false);
	settings->GetAttributeOrDefault("loops", &loops, 1);
	settings->GetAttributeOrDefault("acceptratio", &accept_ratio, 0.3);
	settings->GetAttributeOrDefault("end", &end, 0.001);
	// cooling
	std::string coolingStr = settings->GetAttributeOrDefault("cooling", "Geometric");
	if (!STRCMP(coolingStr.c_str(), "Geometric")) cooling = Geometric;
	else if (!STRCMP(coolingStr.c_str(), "Costa")) cooling = Costa;
	else if (!STRCMP(coolingStr.c_str(), "Hurley")) cooling = Hurley;
	else {
		Log() << "Neplatne nastaveni chlazeni: " << coolingStr << ". Pouzivam Geometric." << std::endl;
		cooling = Geometric;
	}
	initial = settings->FirstChildElement(false);
}

/** Vypise do logu informace o nastaveni algoritmu.
 */
void AnnealingOptions::Print() {
	Log() << GetTitle() << " = nastaveni zihaciho algoritmu: ";
	if (!all) {
		Log() << " chladici strategie = " << CoolingNames[cooling] << ", faktor klesani teploty = " << alpha;
	}
	else {
		Log() << "vsechny varianty chlazeni" << ", faktor klesani teploty = " << alpha;
	}
	Log() << std::endl;
}

/** Vrati kratky popisek nastaveni algoritmu.
 * \return popisek nastaveni 
 */
std::string AnnealingOptions::GetTitle() {
	char title[64];
	sprintf(title, "%sL%.1fA%.2fR%.2fE%.3f", CoolingShort[cooling], loops, alpha, accept_ratio, end);
	return title;
}

/** Nacte dalsi chladici strategii.
 * \return true pokud existuje dalsi strategie
 */
bool AnnealingOptions::Next() {
	/* create next configuration */
	if (all) {
		all = false;
		return true;
	}
	else if (cooling < Hurley) {
		cooling = (CoolingStrategy)(cooling + 1);
		return true;
	}
	else {
		return false;
	}
	return true;
}
